# -*- coding: utf-8 -*-
"""
Created on Fri Nov  9 19:11:02 2018

@author: 陈忠涛
"""
import tushare as ts
import time
import pandas as pd
from dateutil.parser import parse
import matplotlib.pyplot as plt
# 初始化pro接口
pro = ts.pro_api('0089d005171fb2f0b539ef8e8c5c7691471c0beb94ae2ab3315a87ec')

# 数据处理函数
def getData_new(data):
    data.index = range(1,len(data) + 1)
    new_data_reslut = data.drop(['open', 'high', 'low', 'pre_close', 'change', 'pct_change', 'vol', 'amount'], axis = 1)
    new_month = []
    for m in new_data_reslut['trade_date']:
        tra_mon = time.strptime(str(parse(m)),'%Y-%m-%d %H:%M:%S').tm_mon
        new_month.append(tra_mon)
    new_data_reslut['month'] = new_month
    return new_data_reslut

data_changcheng = pro.daily(ts_code='000018.SZ', start_date='20170101', end_date='20181231')  # 神州长城
data_shuma = pro.daily(ts_code = '000034.SZ', start_date='20170101', end_date='20181231') # 神州数码

new_changcheng = getData_new(data_changcheng) # 神州长城
new_shuma = getData_new(data_shuma) # 神州数码

print(new_changcheng)
print(new_shuma)

# plt.title('test')
# print('神州长城',new_changcheng['close'])
# print('神州数码',new_shuma['close'])

# new_changcheng['close'].plot()
# new_shuma['close'].plot(marker = 'v') # 图像属性控制
# new_changcheng['close'].plot()

quo = pd.DataFrame()
quo['changcheng'] = new_changcheng.groupby('month')['close'].sum()
quo['shuma'] = new_shuma.groupby('month')['close'].sum()
# print(type(quo['changcheng']))
quo.plot(kind = 'bar')  # 柱状图
# quo['shuma'].plot(kind = 'pie', subplots = True, autopct = '%.2f') # 圆饼图
# quo.boxplot()  # 箱型图


# 写入csv文件
quo.to_csv('mm.csv')
